package aspect;

import com.example.springframeworklearning.service.OrderGoodsService;
import com.example.springframeworklearning.service.impl.OrderGoodsServiceImpl;
import com.example.springframeworklearning.aspect.introductions.LogIntroductionAspect;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;

/**
 * @author luo
 * @since 2022/9/23 10:55
 */
public class ManualAddAspectProxyConfig {

    /**
     * https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-aspectj-programmatic
     * ⚠️ 要想使得 aspect 代理中的 advice 生效
     * 那么 target 对象就必须符合 aspect 中 切点 的匹配规则
     * 比如：OrderGoodsServiceImpl 和 OrderServiceImpl 放在了同一个包下面
     * OrderGoodsServiceImpl 是以 Order 前缀开头
     */
    @DisplayName("手动为target对象创建 aspect 代理")
    @Test
    public void addAspectProxy(){

        OrderGoodsService goodsService = new OrderGoodsServiceImpl();

        goodsService.updateStock(10);

        AspectJProxyFactory proxyFactory = new AspectJProxyFactory(goodsService);

        proxyFactory.addAspect(LogIntroductionAspect.class);

        OrderGoodsService proxy = proxyFactory.getProxy();

        proxy.updateStock(10);
    }

}
